package com.golddaymicromall.activity.storemanager;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.StoreDetailInfo;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

public class StoreDetailActivity extends BaseActivity implements View.OnClickListener {

    //控件类
    private TextView mStoreNo;
    private TextView mStoreType;
    private TextView mStoreArea;
    private TextView mStoreIdentify;
    private TextView mStoreTggrade;
    private TextView mStoreMobile;
    private TextView mStoreEmail;
    private TextView mStoreQQ;
    private TextView mStoreRegdate;
    private TextView mStoreTurndate;
    private LinearLayout mStoreBack;
    //参数类
    private String logid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_detail);
        initView();
        //开始异步处理
        attemptStoreDetail();
    }

    private void attemptStoreDetail() {
        //获取本地数据
        logid = SPUtils.get(getApplicationContext(), "Id", "").toString();

        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.customer.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("id", logid);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(StoreDetailActivity.this, "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            Toast.makeText(StoreDetailActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            List<StoreDetailInfo> storeDetail = JSON.parseArray(result, StoreDetailInfo.class);
                            //在这里进行返回值的处理
                            mStoreNo.setText(storeDetail.get(0).getCustomerno() + "(" + storeDetail.get(0).getUsername() + ")");
                            mStoreType.setText(storeDetail.get(0).getShoptype());
                            mStoreArea.setText(storeDetail.get(0).getProvince1() + " " + storeDetail.get(0).getCity1() + " " + storeDetail.get(0).getArea1());
                            mStoreIdentify.setText(storeDetail.get(0).getIdentify());
                            mStoreTggrade.setText(storeDetail.get(0).getGradename());
                            mStoreMobile.setText(storeDetail.get(0).getMobile());
                            mStoreEmail.setText(storeDetail.get(0).getEmail());
                            mStoreQQ.setText(storeDetail.get(0).getQq());
                            mStoreRegdate.setText(storeDetail.get(0).getRegdatestr());
                            mStoreTurndate.setText(storeDetail.get(0).getTurndate());
                        }
                    } else {
                        Toast.makeText(StoreDetailActivity.this, "您还没有店铺！！！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                },throwable -> {
                    showError();
                    throwable.printStackTrace();
                });

    }

    public void initView() {
        mStoreNo = (TextView) findViewById(R.id.storeNo);
        mStoreType = (TextView) findViewById(R.id.storeType);
        mStoreArea = (TextView) findViewById(R.id.storeArea);
        mStoreIdentify = (TextView) findViewById(R.id.storeidentify);
        mStoreTggrade = (TextView) findViewById(R.id.storeTggrade);
        mStoreMobile = (TextView) findViewById(R.id.storeMobile);
        mStoreEmail = (TextView) findViewById(R.id.storeEmail);
        mStoreQQ = (TextView) findViewById(R.id.storeQQ);
        mStoreRegdate = (TextView) findViewById(R.id.storeRegDate);
        mStoreTurndate = (TextView) findViewById(R.id.storeTurndate);
        mStoreBack = (LinearLayout) findViewById(R.id.store_back);
        mStoreBack.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.store_back:
                finish();
                break;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
